// ProductServiceImpl.java
package cn.tedu.apps_system.service.impl;

import cn.tedu.apps_system.base.response.ResultCode;
import cn.tedu.apps_system.exception.BusinessException;
import cn.tedu.apps_system.mapper.MerchantMapper;
import cn.tedu.apps_system.mapper.ProductMapper;
import cn.tedu.apps_system.pojo.dto.ProductAddDTO;
import cn.tedu.apps_system.pojo.dto.ProductUpdateDTO;
import cn.tedu.apps_system.pojo.entity.Merchant;
import cn.tedu.apps_system.pojo.entity.Product;
import cn.tedu.apps_system.pojo.vo.ProductVO;
import cn.tedu.apps_system.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public void add(ProductAddDTO productAddDTO) {
        // 检查商家是否存在
        Merchant merchant = merchantMapper.selectById(productAddDTO.getMerchantId());
        if (merchant == null) {
            throw new BusinessException(ResultCode.MERCHANT_NOT_EXIST);
        }

        Product product = new Product();
        BeanUtils.copyProperties(productAddDTO, product);
        product.setStatus(0); // 待审核状态
        product.setSales(0); // 初始销量为0
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());

        productMapper.insert(product);
    }

    @Override
    public void update(ProductUpdateDTO productUpdateDTO) {
        Product product = productMapper.selectById(productUpdateDTO.getId());
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }

        // 检查商家是否匹配
        if (!product.getMerchantId().equals(productUpdateDTO.getMerchantId())) {
            throw new BusinessException(ResultCode.PRODUCT_MERCHANT_MISMATCH);
        }

        BeanUtils.copyProperties(productUpdateDTO, product);
        product.setUpdateTime(new Date());

        productMapper.update(product);
    }

    @Override
    public void delete(Long id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }

        productMapper.deleteById(id);
    }

    @Override
    public void putOnShelf(Long id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }

        // 只有审核通过的商品才能上架
        if (product.getStatus() != 1) {
            throw new BusinessException(ResultCode.PRODUCT_STATUS_ERROR);
        }

        productMapper.updateStatus(id, 1); // 上架状态
    }

    @Override
    public void takeOffShelf(Long id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }

        productMapper.updateStatus(id, 2); // 下架状态
    }

    @Override
    public ProductVO getById(Long id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }

        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public List<ProductVO> listByMerchantId(Long merchantId) {
        List<Product> products = productMapper.selectByMerchantId(merchantId);
        return products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ProductVO> listByMerchantIdAndStatus(Long merchantId, Integer status) {
        List<Product> products = productMapper.selectByMerchantIdAndStatus(merchantId, status);
        return products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
    }

    @Override
    public void reviewProduct(Long id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new BusinessException(ResultCode.PRODUCT_NOT_EXIST);
        }
        if (product.getStatus() != 0) {
            throw new BusinessException(ResultCode.PRODUCT_ALREADY_REVIEWED);
        }

        productMapper.updateStatusById(id, 1); // 更新为已审核状态
    }

    @Override
    public List<ProductVO> listUnreviewedProducts() {
        List<Product> products = productMapper.selectByStatus(0); // 查询 status = 0 的商品
        return products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
    }

}